package com.software.levelupandroid.home.ui.slideshow;

import android.app.DatePickerDialog;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.software.levelupandroid.R;
import com.software.levelupandroid.entity.Badge;
import com.software.levelupandroid.entity.Resource;
import com.software.levelupandroid.entity.SystemTask;
import com.software.levelupandroid.home.adapter.DeleteItemAdapter;
import com.software.levelupandroid.home.adapter.TaskItemAdapter;
import com.software.levelupandroid.home.ui.todayTask.TodayTaskViewModel;
import com.software.levelupandroid.systemTask.model.SystemShowViewModel;
import com.software.levelupandroid.systemTask.presenter.TaskPresenter;
import com.software.levelupandroid.systemTask.view.IView;
import com.software.levelupandroid.systemTask.view.activity.SecondSelectionActivity;
import com.software.levelupandroid.utils.DateUtil;
import com.software.levelupandroid.utils.StringUtil;
import com.software.levelupandroid.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class SystemTaskFragment extends Fragment implements IView {
    private TaskPresenter taskPresenter;
    private RecyclerView recyclerView;
    private SystemShowViewModel systemShowViewModel;
    private TodayTaskViewModel todayTaskViewModel;
    private String dateString = DateUtil.getNowDate("yyyy年MM月dd日");
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.fragment_system_task, container, false);


        taskPresenter = new TaskPresenter(this);
        //绑定控件
        findViews(view);

        //实例化ViewModel实例
        initViewModel();
        //模拟数据
        getData();
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
       return view;
    }

    /**
     * 获取后端的数据
     */
    private void getData() {
        observeLiveData();
    }
    @Override
    public void onResume() {
        super.onResume();
        systemShowViewModel.getAllSystemTask(getActivity());
    }
    private void observeLiveData() {
        systemShowViewModel.getSystemTaskListLiveData().observe(this, new Observer<Resource<List<SystemTask>>>() {
            @Override
            public void onChanged(Resource<List<SystemTask>> listResource) {

                if (listResource != null){
                    if (listResource.isSuccess()){
                        List<SystemTask> data = listResource.getData();

                        //添加装饰
                        recyclerView.addItemDecoration(new MyDecoration());

                        DeleteItemAdapter deleteItemAdapter = new DeleteItemAdapter(getContext(),data,taskPresenter,systemShowViewModel);
                        recyclerView.setAdapter(deleteItemAdapter);
                    }else if (listResource.isFailure()){
                        ToastUtil.showToastOnUI(getActivity(), listResource.getErrorMessage());
                    } else if (listResource.isLoading()) {

                    }
                }
            }
        });

        systemShowViewModel.getBadgeLiveData().observe(this, new Observer<Resource<Badge>>() {
            @Override
            public void onChanged(Resource<Badge> badgeResource) {
                if (badgeResource != null){
                    if (badgeResource.isSuccess()){

                    }else if (badgeResource.isFailure()){
//                        ToastUtil.showToastOnUI(getActivity(), badgeResource.getErrorMessage());
                    } else if (badgeResource.isLoading()) {

                    }
                }
            }
        });
    }

    private void findViews(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.rv_system);

    }
    private void initViewModel() {
        ViewModelProvider viewModelProvider = new ViewModelProvider(this);
        todayTaskViewModel = viewModelProvider.get(TodayTaskViewModel.class);
        systemShowViewModel = viewModelProvider.get(SystemShowViewModel.class);
    }




    @Override
    public void issueTaskSuccess(SystemTask task) {
        ToastUtil.showToastOnUI(getActivity(), "新增任务成功");
    }


    /**
     * IView接口中新增任务失败的方法
     * @param msg
     */
    @Override
    public void issueTaskFailure(String msg) {

        ToastUtil.showToastOnUI(getActivity(), "新增任务失败");

        Log.e("error", msg);
    }

    /**
     * IView接口中删除任务成功
     * @param task
     */
    @Override
    public void deleteTaskSuccess(SystemTask task) {
        ToastUtil.showToastOnUI(getActivity(), "删除任务成功");
    }

    /**
     * IView接口中删除任务失败
     * @param msg
     */
    @Override
    public void deleteTaskFailure(String msg) {
        ToastUtil.showToastOnUI(getActivity(), msg);
    }

    /**
     * IView接口中任务状态更新成功
     * @param task
     */
    @Override
    public void isFinishSuccess(SystemTask task) {
        ToastUtil.showToastOnUI(getActivity(), "任务状态更新成功");
    }

    /**
     * IView接口中任务状态更新失败
     * @param msg
     */
    @Override
    public void isFinishFailure(String msg) {
        ToastUtil.showToastOnUI(getActivity(), msg);
    }



    class MyDecoration extends RecyclerView.ItemDecoration {

        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            //设置间距
            outRect.left = 30;
            outRect.right = 50;
            outRect.top = 15;
            outRect.bottom = 50;
        }
    }
}